package com.jeethink.erp.shop.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

import com.jeethink.common.core.domain.TreeSelect;
import com.jeethink.common.core.domain.entity.TbShop;
import com.jeethink.common.utils.DateUtils;
import com.jeethink.common.utils.StringUtils;
import com.jeethink.erp.shop.domain.TbShopVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.jeethink.erp.shop.mapper.TbShopMapper;
import com.jeethink.erp.shop.service.ITbShopService;

/**
 * 店铺树结构Service业务层处理
 * 
 * @author ypr
 * @date 2021-05-26
 */
@Service
public class TbShopServiceImpl implements ITbShopService 
{
    @Autowired
    private TbShopMapper tbShopMapper;

    /**
     * 查询店铺树结构
     * 
     * @param shopId 店铺树结构ID
     * @return 店铺树结构
     */
    @Override
    public TbShop selectTbShopById(Long shopId)
    {
        return tbShopMapper.selectTbShopById(shopId);
    }

    /**
     * 查询店铺树结构列表
     * 
     * @param tbShop 店铺树结构
     * @return 店铺树结构
     */
    @Override
    public List<TbShop> selectTbShopList(TbShop tbShop)
    {
        return tbShopMapper.selectTbShopList(tbShop);
    }

    /**
     * 新增店铺树结构
     * 
     * @param tbShop 店铺树结构
     * @return 结果
     */
    @Override
    public int insertTbShop(TbShop tbShop)
    {
        tbShop.setCreateTime(DateUtils.getNowDate());
        return tbShopMapper.insertTbShop(tbShop);
    }

    /**
     * 修改店铺树结构
     * 
     * @param tbShop 店铺树结构
     * @return 结果
     */
    @Override
    public int updateTbShop(TbShop tbShop)
    {
        tbShop.setUpdateTime(DateUtils.getNowDate());
        return tbShopMapper.updateTbShop(tbShop);
    }

    /**
     * 批量删除店铺树结构
     * 
     * @param shopIds 需要删除的店铺树结构ID
     * @return 结果
     */
    @Override
    public int deleteTbShopByIds(Long[] shopIds)
    {
        return tbShopMapper.deleteTbShopByIds(shopIds);
    }

    /**
     * 删除店铺树结构信息
     * 
     * @param shopId 店铺树结构ID
     * @return 结果
     */
    @Override
    public int deleteTbShopById(Long shopId)
    {
        return tbShopMapper.deleteTbShopById(shopId);
    }

    @Override
    public List<TreeSelect> buildShopTreeSelect(List<TbShop> tbShops) {
        List<TbShop> shopTrees = buildShopTree(tbShops);
        return shopTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<TbShop> buildShopTree(List<TbShop> tbShops) {
        List<TbShop> returnList = new ArrayList<TbShop>();
        List<Long> tempList = new ArrayList<Long>();
        for (TbShop shop : tbShops)
        {
            tempList.add(shop.getShopId());
        }
        for (Iterator<TbShop> iterator = tbShops.iterator(); iterator.hasNext();)
        {
            TbShop shop = (TbShop) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(shop.getParentId()))
            {
                recursionFn(tbShops, shop);
                returnList.add(shop);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = tbShops;
        }
        return returnList;
    }

    @Override
    public List<TbShop> selectShopChartList(TbShop tbShop) {
        return tbShopMapper.selectShopChartList(tbShop);
    }

    @Override
    public List<TbShopVo> selectCommodityChart(TbShopVo tbShop) {
        return tbShopMapper.selectCommodityChart(tbShop);
    }

    @Override
    public List<TbShopVo> selectVoChart(TbShopVo tbShopVo) {
        return tbShopMapper.selectVoChart(tbShopVo);
    }

    @Override
    public List<TbShop> selectMonthList(TbShop tbShop) {
        return tbShopMapper.selectMonthList(tbShop);
    }

    @Override
    public TbShopVo findCountSize(TbShopVo tbShopVo) {
        return tbShopMapper.findCountSize(tbShopVo);
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<TbShop> list, TbShop t)
    {
        // 得到子节点列表
        List<TbShop> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TbShop tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }


    /**
     * 得到子节点列表
     */
    private List<TbShop> getChildList(List<TbShop> list, TbShop t)
    {
        List<TbShop> tlist = new ArrayList<TbShop>();
        Iterator<TbShop> it = list.iterator();
        while (it.hasNext())
        {
            TbShop n = (TbShop) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId().longValue() == t.getShopId().longValue())
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TbShop> list, TbShop t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

}
